<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
  >
    <el-table :data="formData" class="-mt-10px">
      <el-table-column label="序号" type="index" width="100" />
      <el-table-column label="班次" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.piShiftId`" :rules="formRules.piShiftId" class="mb-0px!">
            <el-select v-model="row.piShiftId" placeholder="请选择班次">
              <el-option
                v-for="dict in getIntDictOptions(DICT_TYPE.PI_SHIFT)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="人员岗位" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.user`" :rules="formRules.user" class="mb-0px!">
            <el-input v-model="row.user" placeholder="请输入人员岗位" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.beginDate`" :rules="formRules.beginDate" class="mb-0px!">
            <el-input v-model="row.beginDate" placeholder="请输入开始时间" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.endDate`" :rules="formRules.endDate" class="mb-0px!">
            <el-input v-model="row.endDate" placeholder="请输入结束时间" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="巡检清单" min-width="180">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.piBomId`" :rules="formRules.piBomId" class="mb-0px!">
            <el-select
              v-model="row.piBomId"
              multiple
              clearable
              filterable
              placeholder="请选择巡检清单"
            >
              <el-option
                v-for="item in BomList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="楼栋" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.houseId`" :rules="formRules.houseId" class="mb-0px!">
            <el-select
              v-model="row.houseId"
              multiple
              clearable
              filterable
              placeholder="请选择房产档案"
            >
              <el-option
                v-for="item in HouseList"
                :key="item.id"
                :label="item.name"
                :value="item.id!"
              />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>

      <el-table-column align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button @click="handleDelete($index)" link>—</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <el-row justify="center" class="mt-3">
    <el-button @click="handleAdd" round>+ 添加PI 排班套餐明细</el-button>
  </el-row>
</template>
<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { ConfigureComboApi } from '@/api/pi/configurecombo'
import { BomApi, BomVO } from '@/api/pi/bom'
import * as HouseSpuApi from '@/api/house/house/spu'

const props = defineProps<{
  piConfigureComboId: number // 排班套餐id（主表的关联字段）
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref<any[]>([])
const formRules = reactive({
  piConfigureComboId: [{ required: true, message: '排班套餐id不能为空', trigger: 'blur' }],
  piShiftId: [{ required: true, message: '班次id不能为空', trigger: 'change' }],
  user: [{ required: true, message: '人员岗位不能为空', trigger: 'blur' }],
  houseId: [{ required: true, message: '房屋id不能为空', trigger: 'blur' }],
  piBomId: [{ required: true, message: '档案id不能为空', trigger: 'blur' }],
  beginDate: [{ required: true, message: '开始时间不能为空', trigger: 'blur' }],
  endDate: [{ required: true, message: '结束时间不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref
const BomList = ref<BomVO[]>([]) // 巡检清单列表
const HouseList = ref<HouseSpuApi.Spu[]>([]) // 楼栋资料列表

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.piConfigureComboId,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      return
    }
    try {
      formLoading.value = true
      formData.value = await ConfigureComboApi.getConfigureComboDetailListByPiConfigureComboId(val)
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    id: undefined,
    piConfigureComboId: undefined,
    piShiftId: undefined,
    user: undefined,
    houseId: undefined,
    piBomId: undefined,
    beginDate: undefined,
    endDate: undefined
  }
  row.piConfigureComboId = props.piConfigureComboId as any
  formData.value.push(row)
}

/** 删除按钮操作 */
const handleDelete = (index) => {
  formData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}

/** 表单值 */
const getData = () => {
  return formData.value
}

defineExpose({ validate, getData })
/** 初始化 */
onMounted(async () => {
  BomList.value = await BomApi.getBomSimpleList()
  HouseList.value = await HouseSpuApi.getSpuSimpleList()
})
</script>
